package org.example.erp.controller;
import lombok.RequiredArgsConstructor;
import org.example.erp.domain.WmProductSalse;
import org.example.erp.domain.WmProductSalseLine;
import org.example.erp.service.WmProductSalseLineService;
import org.example.erp.service.WmProductSalseService;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class WmProductSalesController {
    private final WmProductSalseService wmProductSalseService;
    private final WmProductSalseLineService wmProductSalseLineService;

    // 新增销售出库单
    @PostMapping("/salesOutbound/insert")
    public DataResult saveSalesOutbound(@RequestBody WmProductSalse wmProductSalse) {
        wmProductSalseService.saveSalesOutbound(wmProductSalse);
        return DataResult.success();
    }

    // 查询销售出库单
    @GetMapping("/salesOutbound/find")
    public DataResult findSalesOutbound(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                        @RequestParam(required = false) String salesCode,
                                        @RequestParam(required = false) String soCode,
                                        @RequestParam(required = false) String clientName, @RequestParam(required = false) String salseDate) {
        return DataResult.success(wmProductSalseService.findSalesOutbound(pageNum, pageSize, salesCode, soCode, clientName, salseDate));
    }
    // 删除销售出库单
    @DeleteMapping("/salesOutbound/delete")
    public DataResult deleteSalesOutbound(@RequestBody List<Long> salseIds) {
        wmProductSalseService.deleteSalesOutbound(salseIds);
        return DataResult.success();
    }

    // 查询单个销售出库单
    @GetMapping("/salesOutbound/query")
    public DataResult querySalesOutbound(@RequestParam Long salesId){
        return DataResult.success(wmProductSalseService.querySalesOutbound(salesId));
    }

    @PutMapping("/salesOutbound/update")
    public DataResult updateSalesOutbound(@RequestBody WmProductSalse wmProductSalse){
        wmProductSalseService.updateById(wmProductSalse);
        return DataResult.success();
    }

    // 执行产品出库
    @PutMapping("/salesOutbound/shipments")
    public DataResult shipments(@RequestBody WmProductSalse wmProductSalse){
        wmProductSalseService.shipments(wmProductSalse);
        return DataResult.success();
    }

    // 添加销售出库行数据
    @PostMapping("/salesOutboundLine/insert")
    public DataResult insertSalesOutboundLine(@RequestBody WmProductSalseLine wmProductSalseLine){
        wmProductSalseLineService.save(wmProductSalseLine);
        return DataResult.success();
    }

    // 删除销售订单产品列信息
    @DeleteMapping("/salesOutboundLine/delete")
    public DataResult deleteSalesOutboundLine(@RequestBody List<Long> lineIds){
        wmProductSalseLineService.removeByIds(lineIds);
        return DataResult.success();
    }
}
